<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-01-29
 * Time: 15:31
 */

namespace app\index\controller;

header("Access-Control-Allow-Origin:*");

header('Access-Control-Allow-Methods:*');

header('Access-Control-Allow-Headers:*');
use app\common\controller\HomeBase;
use app\common\model\Feedbackm;
use app\common\model\File;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;

class Feedback extends HomeBase
{
    private $arr = ['JPG', 'JPEG', 'PNG', 'GIF', 'jpg', 'jpeg', 'png', 'gif'];
    private $size = 5250000;//上传的大小
    /**
     * 添加用户意见反馈
     */
    public function addFeedBack()
    {
        if (request()->isPost()) {
            $params = input();
            $validate_result = $this->validate($params, 'Wfeedback');
            if ($validate_result !== true) {
                return  json(['code' => 300, 'msg' => $validate_result]);
            }
            $status =  Feedbackm::addFeedback($params);
            if ($status) {
                return json(['code' => 200, 'msg' => '添加成功']);
            } else {
                return json(['code' => 300, 'msg' => '添加失败']);
            }
        } else {
            return json(['code' => 300, 'msg' => '请求方式错误']);
        }
    }

    /**前端  多图上传。
     * @return mixed|\think\response\Json|void
     */
    public function uploadImgs()
    {
//        if (request()->isPost()) {
//            $files = request()->file('files');
//            $params = input();
//            if(array_key_exists("uid",$params) && $params['uid'] != ''){
//                $uid = input()['uid'];
//            } else {
//                $uid = '';
//            }
//            if (!$files) {
//                return $this->error('请上传文件');
//            }
//            $firstName = $_FILES['files']['name'];
//
//            $nameObj = $_FILES['files']['tmp_name'];
//            $sizeArr = $_FILES['files']['size'];
//            $dir = iconv("UTF-8", "GBK", "uploads/files/");
//            if (!file_exists($dir)) {
//                mkdir($dir, 0777,true);
//            }
//            $imgArray = [];
//
//            for ($i = 0; $i < count($firstName); $i++) {
//                if ($firstName[$i] != '') {
//                    $nameArr = explode('.', $firstName[$i]);
//
//                    $lastName = end($nameArr);
//                    if ($sizeArr[$i] > $this->size) {
//                        return json(['code' => 0, 'msg' => '文件太大，上传失败']);
//                    }
//
//                    if(in_array($lastName, $this->arr)) {
//                        $nameHost =  date('YmdHis', time()) . md5(uniqid()) .'.'. $lastName;
//                        $saveName = '/uploads/files/'.$nameHost;
////                        $saveName = $_SERVER['DOCUMENT_ROOT'].'/uploads/files/'.$nameHost;
//                        move_uploaded_file($nameObj, $saveName);
//                        $fId = File::addAddress($nameArr[0], $saveName, $lastName, $uid);
//                        if ($fId) {
//                            array_push($imgArray, $fId);
//                        }
//                    } else {
//                        return json(['code' => 0, 'msg' => '文件格式不正确，上传失败']);
//                    }
//                }
//            }
//            return json(['code' => 1, 'msg' => $imgArray]);
//        }else {
//            return $this->fetch();
//        }
        if (request()->isPost()) {
            $params = input();
            $files = request()->file('files');
            if (!$files) {
                return json(['code' => 301, 'msg' => '请上传文件']);
            }
            if(array_key_exists("uid",$params) && $params['uid'] != ''){
                $uid = input()['uid'];
            } else {
                $uid = '';
            }
            $firstName = $_FILES['files']['name'];
            $nameObj = $_FILES['files']['tmp_name'];
            $sizeArr = $_FILES['files']['size'];
            $dir = iconv("UTF-8", "GBK", "uploads/files/");
            if (!file_exists($dir)) {
                mkdir($dir, 0777,true);
            }
            if ($firstName) {
                $nameArr = explode('.', $firstName);
                $lastName = end($nameArr);
                if ($sizeArr > $this->size) {
                    return json(['code' => 301, 'msg' => '文件太大，上传失败']);
                }
                if(in_array($lastName, $this->arr)) {
                    $nameHost =  date('YmdHis', time()) . md5(uniqid()) .'.'. $lastName;
                    $saveName = $_SERVER['DOCUMENT_ROOT'].'/uploads/files/'.$nameHost;
//                    $saveName = $_SERVER['HTTP_HOST'].'/uploads/files/'.$nameHost;

                    move_uploaded_file($nameObj, $saveName);
//                    $new_name = '/uploads/files/'.$nameHost;
                    $fId = File::addAddress($nameArr[0],'/uploads/files/'.$nameHost, $lastName, $uid);

                    if ($fId) {
                        return json(['code' => 200, 'msg' => '文件上传成功', 'fid' => $fId, 'name' =>$nameArr[0], 'type' => $lastName, 'newAddress' => '/uploads/files/'.$nameHost]);
                    }
                } else {
                    return json(['code' => 301, 'msg' => '文件格式不正确，上传失败']);
                }
            } else {
                return json(['code' => 301, 'msg' => '请选择要传的的文件']);
            }
        }else {
            return json(['code' => 301, 'msg' => '请求方式错误']);

        }
    }

    /**
     * 根据文件id  删除文件
     */
    public function delFile()
    {
        $params = input();
        if(array_key_exists("fid",$params) && $params['fid'] != ''){
            if (File::delFile($params['fid'])) {
                return json(['code' => 200, 'msg' => '删除成功']);
            } else {
                return json(['code' => 300, 'msg' => '删除失败']);
            }
        } else {
            return json(['code' => 300, 'msg' => '请选择要删除的文件']);
        }

    }
}